package gamecontent;

import interaction.MoveBehaviour;
import interaction.shootBehaviour;

import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;

import javax.media.j3d.Appearance;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.Node;
import javax.media.j3d.Texture;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.swing.JFrame;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3f;

import light.DefaultLight;
import light.DirLight;
import light.SpLight;
import modell.Board;
import modell.Canon;
import modell.Canonball;
import modell.Koordinatenkreuz;

import collision.FrogCollision;
import collision.NavigationCollision;

import com.sun.j3d.utils.behaviors.vp.OrbitBehavior;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.geometry.Primitive;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.picking.PickResult;
import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.universe.ViewingPlatform;

public class Game extends JFrame{
	
	private SimpleUniverse myUniverse = null;
	private Canvas3D myDrawTable = null;
	private BranchGroup root = null;
	private ViewingPlatform vp = null;
	private PickResult result = null;
	
	public Game(){
		initWorld();
		fillBranchGroup();
//		addBehavior();
		displayWorld();
	}
	
	private void initWorld(){
		//Welt aufbauen
		GraphicsConfiguration myConfig = SimpleUniverse.getPreferredConfiguration();
		myDrawTable = new Canvas3D(myConfig);
		myUniverse = new SimpleUniverse(myDrawTable);
//		myUniverse.getViewingPlatform().setNominalViewingTransform();
		
		// Set camera position and orientation.
		vp = myUniverse.getViewingPlatform();
		TransformGroup view_transform_group = 
				vp.getMultiTransformGroup().getTransformGroup(0);
		Transform3D view_transform_3D = new Transform3D();
        view_transform_group.getTransform(view_transform_3D);
        view_transform_3D.setTranslation(new Vector3f(0.0f,-10.0f,10.0f)); 
        Transform3D view_rot_y = new Transform3D();
        //view_rot_y.rotY(Math.PI/4);
        Transform3D view_rot_x = new Transform3D();
        //view_rot_x.rotX(Math.toRadians(80));
        view_transform_3D.mul(view_rot_y);
        view_transform_3D.mul(view_rot_x);
        view_transform_group.setTransform(view_transform_3D);
	}
	
	private void fillBranchGroup(){
		root = new BranchGroup();
		root.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
		root.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
		
		//Mit Objekten fuellen
//		root.addChild(new Koordinatenkreuz());
		root.addChild(new DefaultLight());
		root.addChild(new DirLight());
//		root.addChild(new SpLight());
	 	root.addChild(new Board());
//		root.addChild(new Bump());
		
		//Hintergrund Sphere
		Transform3D tz = new Transform3D();
		tz.rotZ(Math.toRadians(180));
		Transform3D tx = new Transform3D();
		tx.rotX(Math.toRadians(-20));
		Transform3D ty = new Transform3D();
		ty.rotY(Math.toRadians(180));
		tz.mul(tx);
		tz.mul(ty);
		TransformGroup tg = new TransformGroup(tz);
		Appearance app = new Appearance(); 
		Texture tex = new TextureLoader( "textures/Green_grass_and_blue_sky_by_s1lv3r_bg.jpg", null).getTexture();
	    app.setTexture( tex );
	    Sphere sphere = new Sphere( 32f,Primitive.GENERATE_TEXTURE_COORDS |Primitive.GENERATE_NORMALS_INWARD, app );
	    tg.addChild(sphere);
	    tg.setCollidable(false);
	    root.addChild(tg);
	    
		//Frosch + Animation + Verschiebung
		Frogheads frogheads = new Frogheads(this);


		root.addChild(frogheads);
		
		//Behavior zum schiessen, Target ist die Froschgruppe
		//Test
//		BranchGroup bg = new BranchGroup();
//		ColorCube myCube = new ColorCube(0.2d);
//		myCube.setCapability(Node.ENABLE_PICK_REPORTING);
//		myCube.setPickable(true);
//		bg.addChild(myCube);
//		bg.setPickable(true);
//		bg.setCapability(BranchGroup.ENABLE_PICK_REPORTING);
		root.addChild(new shootBehaviour(this.root, 
				frogheads, 
				this.vp));
//		root.addChild(bg);
		
		//TG der Kanone und der Kamera um die Bewegung zu steuern
		TransformGroup canonTG = new TransformGroup();
		canonTG.addChild(new Canon());
		root.addChild(new MoveBehaviour(this.vp.getViewPlatformTransform(),
				canonTG));
		root.addChild(canonTG);
		
		//NavigationsgrenzenBehavior
		root.addChild(new NavigationCollision(tg, 
				this.vp.getViewPlatformTransform(), 
				canonTG));

		root.compile();
	}
	
	private void addBehavior(){
		OrbitBehavior myBehavior = new OrbitBehavior(myDrawTable, OrbitBehavior.REVERSE_ALL);
		myBehavior.setSchedulingBounds(new BoundingSphere());
		myUniverse.getViewingPlatform().setViewPlatformBehavior(myBehavior);
	}
	
	private void displayWorld(){
		myUniverse.addBranchGraph(root);
		this.getContentPane().setLayout(new BorderLayout());
		this.getContentPane().add("Center", myDrawTable);
		this.setSize(500, 500);
		this.setVisible(true);
	}
}
